package com.alstudio.module.xmpp.distributor.distributor;

import com.alstudio.event.ALEventDeliver;
import com.alstudio.module.xmpp.distributor.distributor.DistributorEvent.CheckDistributorState;
import com.alstudio.module.xmpp.distributor.versioncheck.VersionCheckUtils;
import com.alstudio.pojo.module.server.ServerInfo;
import com.alstudio.utils.log.ALLog;

public class DistributorManager implements DistributorEventListener {

	private static DistributorManager instance;

	// 回调到调用对象

	public DistributorManager() {
		// TODO Auto-generated constructor stub
		VersionCheckUtils.getInstance().setDistributorEventListener(this);
	}

	public static DistributorManager getInstance() {
		if (instance == null) {
			instance = new DistributorManager();
		}
		return instance;
	}

	public void checkDistributor(String currentVersion) {

		final String msg = "<query platform=\"Android\" version=\""
				+ currentVersion + "\"/>";
		ALLog.d("write to server by socket connection: " + msg);

		VersionCheckUtils.getInstance().checkVersion("116.254.203.70", "9225",
				msg, false, false);
	}

	public void checkDistributor(String currentVersion, boolean bySocket) {
		final String msg = "<query platform=\"Android\" version=\""
				+ currentVersion + "\"/>";
		ALLog.d("write to server by socket connection: " + msg);
		VersionCheckUtils.getInstance().checkVersion("116.254.203.70", "9225",
				msg, bySocket, false);
	}

	public void checkDistributor(String currentVersion,String platform,String ip,String port, boolean bySocket) {
		String msg = "<query platform=\"Android\" version=\""
				+ currentVersion + "\"/>";
		
		msg = msg.replace("Android", platform);
		ALLog.d("write to server by socket connection: " + msg);
		VersionCheckUtils.getInstance().checkVersion(ip, port,
				msg, bySocket, false);
	}
	
	public void checkNewVersion(String currentVersion,String platform,String ip,String port) {
		String msg = "<query platform=\"Android\" version=\""
				+ currentVersion + "\"/>";
		
		msg = msg.replace("Android", platform);
		ALLog.d("write to server by socket connection: " + msg);
		VersionCheckUtils.getInstance().checkVersion(ip, port,
				msg, true, true);
	}
	
	@Override
	public void onConnectSuccess() {
		// TODO Auto-generated method stub
		ALLog.d("检测分发器成功");
		DistributorEvent event = createDistributorEvent();
		event.setCheckingState(CheckDistributorState.STATE_CONNECT_SUCCESSED);
		ALEventDeliver.getDefaultAlEventDeliver().postALEvent(event);
	}

	@Override
	public void onConnectFail(String reason) {
		// TODO Auto-generated method stub
		ALLog.e("检测分发器失败");
		DistributorEvent event = createDistributorEvent();
		event.setCheckingState(CheckDistributorState.STATE_CONNECT_FAILED);
		ALEventDeliver.getDefaultAlEventDeliver().postALEvent(event);
	}

	@Override
	public void onCheckDistributorSuccess(ServerInfo info) {
		// TODO Auto-generated method stub

//		if (info != null) {
//
//			BaseActivity a = ALActivityManager.getALActivityManager()
//					.getCurrentActivity();
//			if (a == null) {
//				return;
//			}
//		}
		ALLog.d("检测分发器成功"+info.getLatestVer());
		DistributorEvent event = createDistributorEvent();
		event.setCheckingState(CheckDistributorState.STATE_CONNECT_CHECK_SUCCESSED);
		event.setCheckedServerInfo(info);
		ALEventDeliver.getDefaultAlEventDeliver().postALEvent(event);
	}

	@Override
	public void onCheckDistributorFailed(String reason) {
		// TODO Auto-generated method stub
//		BaseActivity a = ALActivityManager.getALActivityManager()
//				.getCurrentActivity();
//		if (a == null) {
//			return;
//		}
		ALLog.e("检测分发器失败 "+reason);
		DistributorEvent event = createDistributorEvent();
		event.setCheckingState(CheckDistributorState.STATE_CONNECT_CHECK_FAILED);
		event.setFailReason(reason);
		ALEventDeliver.getDefaultAlEventDeliver().postALEvent(event);
		
	}
	
	private DistributorEvent createDistributorEvent(){
		DistributorEvent event = new DistributorEvent();
		return event;
	}

}
